﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.Base;
using Ext.Net;
using MemberMgr.Data.ManagerObjects;
using System.Data;

namespace MemberMgr.Website.Admin
{
    public partial class ProductsIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest && !this.IsPostBack)
            {
                this.txtSearchDateBeg.Value = DateTime.Now;
                this.txtSearchDateBeg.Editable = false;

                this.txtSearchDateEnd.Value = DateTime.Now;
                this.txtSearchDateEnd.Editable = false;
            }
        }

        [DirectMethod]
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataSource();
        }

        [DirectMethod]
        protected void storeProduct_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            this.BindDataSource();
        }

        private void BindDataSource()
        {
            string productName = this.txtSearchName.Text;
            DateTime? dateBeg = null;
            DateTime? dateEnd = null;
            string username = string.Empty;

            if (string.IsNullOrEmpty(this.txtSearchDateBeg.Value.ToString()))
            {
                dateBeg = (DateTime)this.txtSearchDateBeg.Value;
            }
            if (string.IsNullOrEmpty(this.txtSearchDateEnd.Value.ToString()))
            {
                dateEnd = (DateTime)this.txtSearchDateEnd.Value;
            }

            ItProductInlogManager productInMgr = Common.CommonUtil.managerFactory.GettProductInlogManager();
            ItUserManager userMgr = Common.CommonUtil.managerFactory.GettUserManager();

            IList<tUser> userlist = userMgr.GetAll();

            IList<tProductInlog> list = null;

            if (!string.IsNullOrEmpty(productName))
            {
                list = productInMgr.GetProductInLog(productName, dateBeg, dateEnd);
            }
            else
            {
                list = productInMgr.GetAll();
            }

            if (list != null)
            {
                DataTable dt = this.GetMemberInfoDataTable();

                foreach (tProductInlog item in list)
                {
                    
                    tUser user = userlist.Where(p => p.Id == item.Inmember).ToList().FirstOrDefault();
                    
                    if (user != null)
	                {
                        username = user.Username;
	                }

                    dt.Rows.Add(
                        item.Id,
                        item.TProduct.Productname,
                        item.TProduct.Producttype,
                        item.Insum,
                        item.Intime,
                        item.TProduct.Price,
                        username
                        );
                }

                this.storeProduct.DataSource = dt;
                this.storeProduct.DataBind();
            }
        }

        private DataTable GetMemberInfoDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[] { 
                new DataColumn() { ColumnName="ID",DataType=typeof(uint)},
                new DataColumn() { ColumnName="PRODUCTNAME",DataType=typeof(string)},
                new DataColumn() { ColumnName="PRODUCTTYPE",DataType=typeof(string)},
                new DataColumn() { ColumnName="INSUM",DataType=typeof(decimal)},
                new DataColumn() { ColumnName="INTIME",DataType=typeof(DateTime)},
                new DataColumn() { ColumnName="PRICE",DataType=typeof(double)},
                new DataColumn() { ColumnName="INMEMBER",DataType=typeof(string)}
            });

            return dt;
        }

    }
}
